TableCsvFactory   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 18
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A toCsv 0 14 1
1
import { Inject, Injectable } from '@nestjs/common';
2
import { ITranslator } from '../Translations/ITranslator';
3
import { Table } from './';
4
5
@Injectable()
6
export class TableCsvFactory {
7
  constructor(
8
    @Inject('ITranslator')
9
    private translator: ITranslator
10
  ) {}
11
12
  public toCsv(table: Table): string {
13
    const header = table.columns.map(column =>
14
      this.translator.translate(column.toString())
15
    );
16
    const rows = table.rows.map(row =>
17
      row
18
        .map(cell => {
19
          const text = cell.renderText();
20
          return `"${text}"`; // Use quotes to allow newlines
21
        })
22
        .join(';')
23
    );
24
    return [header.join(';'), ...rows].join('\n');
25
  }
26
}
27